﻿@model List<SnongPhoneShop.Models.SanPham>
@{
    TempData.Clear();
    TempData["SortModel"] = ViewBag.SortModel;
    TempData.Keep("SortModel");
}

<div id="search-result-product">
    @if (Model != null) {
        if (Model.Count > 0)
        {
            foreach (var Item in Model)
            { 
                <a href="@Url.Action("CT_SanPham", "San_Pham", new { ID = Item.MaSanPham.Trim() })">
                    <div class="result-product">
                        <img src="@Item.AnhDaiDien" class="quick-result-img-product" />
                        <div class="quick-result-info-product">
                            <div class="quick-result-name-product">@Item.TenSanPham</div>
                            <div class="quick-result-price-product">@Item.DonGia.ToString("n0") VNĐ</div>
                        </div>
                    </div>
                </a>
            }
        }
        else { 
            <div>Không có sản phẩm tương thích</div>
        }
    }
</div>
<div class="search-paging">
    <div class="paging-div">
        <script type="text/javascript">
            function gotoPage(i) {
                $.ajax({
                    url: '/Tim_Kiem/GotoPage',
                    data: { page: i },
                    success: function (html) {
                        $('#search-result-panel').html(html);
                        window.history.pushState('Searchpage', 'SearchPage', 'Tim_Kiem?' + changePageParam(i));
                    },
                    cache: false
                });
            };

            function changePageParam(pageNum) {
                var params = window.location.href.split('?')[1];
                var items = params.split('&');
                var i = 0;
                for (i; i < items.length; i++) {
                    var x = items[i].split('=');
                    if (x[0] == 'page') {
                        x[1] = pageNum;
                        items[i] = x.join('=');
                        params = items.join('&');
                        return params;
                    }
                }
                if (i == items.length) {
                    var newParam = '&page=' + pageNum;
                    params += newParam;
                    return params;
                }
            };
        </script>
        @if (Model != null) { 
            if (ViewBag.pageIndex > 1)
            { 
                <a href="javascript:gotoPage(1)">
                    <span class="page-index first-page-index"></span>
                </a>
                <a href="javascript:gotoPage(@(ViewBag.pageIndex - 1))">
                    <span class="page-index prev-page-index"></span>
                </a>
            }
            else { 
                <a class="disable-link"><span class="page-index first-page-index"></span></a>
                <a class="disable-link"><span class="page-index prev-page-index"></span></a>
            }
            for (int i = ViewBag.startPage; i <= ViewBag.endPage; i++)
            {
                if (i == ViewBag.pageIndex)
                { 
                    <a><span class="page-index curr-page-index">@i</span></a>
                }
                else { 
                    <a href="javascript:gotoPage(@i)"><span class="page-index">@i</span></a>
                }
            }
            if (ViewBag.pageIndex < ViewBag.pageCount)
            { 
                <a href="javascript:gotoPage(@(ViewBag.pageIndex + 1))">
                    <span class="page-index next-page-index"></span>
                </a>
                <a href="javascript:gotoPage(@ViewBag.pageCount)">
                    <span class="page-index last-page-index"></span>
                </a>
            }
            else { 
                <a class="disable-link"><span class="page-index next-page-index"></span></a>
                <a class="disable-link"><span class="page-index last-page-index"></span></a>
            }
        }
    </div>
</div>